package com.yuebook.smartrpc.framework.serialization.serializer.impl;

import com.alibaba.fastjson.JSONObject;
import com.yuebook.smartrpc.framework.serialization.serializer.ISerializer;

/**
 * fastjson序列化-反序列化机制
 * @author honglei
 * @date 2018/11/16 下午3:43
 */
public class FastJsonSerializer implements ISerializer {

    @Override
    public <T> byte[] serialize(T obj) {
        if (obj == null) {
            return new byte[0];
        }
        return JSONObject.toJSONString(obj).getBytes();
    }

    @Override
    public <T> T deserialize(byte[] data, Class<T> clazz) {
        return JSONObject.parseObject(new String(data), clazz);
    }
}
